package com.example.administrator.yuejian35.Recommend.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.yuejian35.Club.bean.Course;
import com.example.administrator.yuejian35.R;
import com.example.administrator.yuejian35.Recommend.adapter.AllcourseListAdapter;
import com.example.administrator.yuejian35.Recommend.model.RecommendModel;
import com.example.administrator.yuejian35.base.BaseFragment;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class ThirdFragment extends BaseFragment {
    private RecyclerView course_rv;
    private List<Course> list;

    public ThirdFragment() {
        // Required empty public constructor
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        course_rv= (RecyclerView) view.findViewById(R.id.course_rv);
        RecommendModel recommendmodel = new RecommendModel();
        Call call = recommendmodel.getcainiaocourses();
        call.enqueue(new Callback<List<Course>>() {
            @Override
            public void onResponse(Call<List<Course>> call, Response<List<Course>> response) {
                if(getActivity()!=null) {
                    list = response.body();
                    AllcourseListAdapter adapter = new AllcourseListAdapter(getActivity(), list);
                    int spanCount = 2;
                    StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(spanCount,
                            StaggeredGridLayoutManager.VERTICAL);
                    course_rv.setLayoutManager(layoutManager);
                    course_rv.setAdapter(adapter);
                    course_rv.setItemAnimator(new DefaultItemAnimator());
                }
            }

            @Override
            public void onFailure(Call<List<Course>> call, Throwable t) {
                Toast.makeText(getActivity(),"查询失败", Toast.LENGTH_LONG).show();
            }
        });
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_third, container, false);
    }

}
